
// 通过Promise封装ajax 将请求和结果处理分离开来

function request(url, params = {}, type = "get") {
    return new Promise(function (resolve, reject) {
        $.ajax({
            type: type,
            url: url,
            data: params,
            dataType: "json",
            success: function (res) {
                resolve(res); //请求成功 => {fulfilled:res}
            },
            error: function (err) {
                reject(err); // 代码出错 => {rejected:err}
            }
        })
    })
    // 调用此方法返回 Promise实例:{pending:undefined} => 请求有结果之后在改变状态
}


const register = params => request("../php/register.php", params, "post");
const isExistUser = params => request("../php/isExistUser.php", params, "get");
const isExistPhone = params => request("../php/isExistPhone.php", params, "get");
const isExistEmail = params => request("../php/isExistEmail.php", params, "get");
const login = params => request("../php/login.php", params, "post");


const searchAllGrade = params => request("../php/searchAllGrade.php", params, "get");
const deleteGradeById = params => request("../php/deleteGradeById.php", params, "get");
const searchGradeById = params => request("../php/searchGradeById.php", params, "get");
const addGrade = params => request("../php/addGrade.php", params, "get");
const updateGradeById = params => request("../php/updateGradeById.php", params, "post");

const searchAllGoods = params => request("../php/searchAllGoods.php", params, "get");
const searchGoodsById = params => request("../php/searchGoodsById.php", params, "get");
const addToShoppingCar = params => request("../php/addToShoppingCar.php", params, "post");
const searchShoppingCarByUser = params => request("../php/searchShoppingCarByUser.php", params, "post");